<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="back-button" @tap="goBack">
						<text class="back-icon">←</text>
					</view>
					<view class="header-info">
						<text class="tool-icon">❤️</text>
						<text class="tool-title">健康占卜</text>
						<text class="tool-subtitle">健康状况和养生建议</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">个人信息</text>
						
						<view class="form-item">
							<text class="form-label">性别</text>
							<view class="gender-selector">
								<view 
									class="gender-item"
									:class="{ active: params.gender === 'male' }"
									@tap="selectGender('male')"
								>
									<text class="gender-icon">👨</text>
									<text class="gender-text">男</text>
								</view>
								<view 
									class="gender-item"
									:class="{ active: params.gender === 'female' }"
									@tap="selectGender('female')"
								>
									<text class="gender-icon">👩</text>
									<text class="gender-text">女</text>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<text class="form-label">年龄</text>
							<picker 
								mode="selector" 
								:range="ageRanges" 
								:value="ageIndex"
								@change="onAgeChange"
								class="age-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayAge }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">关注部位</text>
							<view class="body-parts">
								<view 
									v-for="part in bodyParts" 
									:key="part.value"
									class="part-item"
									:class="{ active: params.bodyPart === part.value }"
									@tap="selectBodyPart(part.value)"
								>
									<text class="part-icon">{{ part.icon }}</text>
									<text class="part-name">{{ part.name }}</text>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<text class="form-label">症状描述（可选）</text>
							<textarea 
								v-model="params.symptoms" 
								placeholder="请简单描述您关心的健康问题"
								class="symptoms-input"
								maxlength="200"
							></textarea>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '分析中...' : '开始分析'" 
						type="primary"
						@tap="queryHealth"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="healthResult">
					<view class="result-section">
						<text class="section-title">健康分析</text>
						
						<view class="health-analysis">
							<view class="overall-health">
								<view class="health-score">
									<view class="score-circle">
										<text class="score-text">{{ healthResult.overall.score }}</text>
										<text class="score-unit">分</text>
									</view>
									<text class="score-level">{{ healthResult.overall.level }}</text>
								</view>
								<view class="health-desc">
									<text class="desc-text">{{ healthResult.overall.description }}</text>
								</view>
							</view>
							
							<view class="health-details">
								<view 
									v-for="(detail, index) in healthResult.details" 
									:key="index"
									class="detail-item"
								>
									<view class="detail-header">
										<text class="detail-icon">{{ detail.icon }}</text>
										<text class="detail-title">{{ detail.title }}</text>
									</view>
									<text class="detail-content">{{ detail.content }}</text>
									<view class="detail-level" :class="detail.level.toLowerCase()">
										<text class="level-text">{{ detail.level }}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 养生建议 -->
				<app-card v-if="healthResult && healthResult.suggestions">
					<view class="suggestions-section">
						<text class="section-title">养生建议</text>
						
						<view class="suggestions-tabs">
							<view 
								v-for="(tab, index) in suggestionTabs" 
								:key="index"
								class="tab-item"
								:class="{ active: activeSuggestionTab === index }"
								@tap="switchSuggestionTab(index)"
							>
								<text class="tab-icon">{{ tab.icon }}</text>
								<text class="tab-name">{{ tab.name }}</text>
							</view>
						</view>
						
						<view class="suggestions-content">
							<view 
								v-for="(suggestion, index) in currentSuggestions" 
								:key="index"
								class="suggestion-item"
							>
								<view class="suggestion-header">
									<text class="suggestion-title">{{ suggestion.title }}</text>
									<view class="suggestion-priority" :class="suggestion.priority">
										<text class="priority-text">{{ getPriorityText(suggestion.priority) }}</text>
									</view>
								</view>
								<text class="suggestion-content">{{ suggestion.content }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 注意事项 -->
				<app-card v-if="healthResult && healthResult.warnings">
					<view class="warnings-section">
						<text class="section-title">注意事项</text>
						
						<view class="warnings-list">
							<view 
								v-for="(warning, index) in healthResult.warnings" 
								:key="index"
								class="warning-item"
							>
								<view class="warning-icon">
									<text class="icon-text">⚠️</text>
								</view>
								<text class="warning-text">{{ warning }}</text>
							</view>
						</view>
						
						<view class="disclaimer">
							<text class="disclaimer-text">本分析仅供参考，如有健康问题请及时就医。</text>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'

export default {
	name: 'HealthToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				gender: '',
				age: '',
				bodyPart: '',
				symptoms: ''
			},
			healthResult: null,
			loading: false,
			toolsService: null,
			ageIndex: 0,
			ageRanges: [
				'18-25岁', '26-35岁', '36-45岁', '46-55岁', '56-65岁', '65岁以上'
			],
			bodyParts: [
				{ value: 'head', name: '头部', icon: '🧠' },
				{ value: 'heart', name: '心脏', icon: '❤️' },
				{ value: 'lung', name: '肺部', icon: '🫁' },
				{ value: 'stomach', name: '肠胃', icon: '🍎' },
				{ value: 'liver', name: '肝脏', icon: '🟫' },
				{ value: 'kidney', name: '肾脏', icon: '🫘' },
				{ value: 'bone', name: '骨骼', icon: '🦴' },
				{ value: 'skin', name: '皮肤', icon: '✨' },
				{ value: 'overall', name: '整体', icon: '🏃' }
			],
			suggestionTabs: [
				{ name: '饮食', icon: '🍎' },
				{ name: '运动', icon: '🏃' },
				{ name: '作息', icon: '😴' },
				{ name: '调理', icon: '🌿' }
			],
			activeSuggestionTab: 0
		}
	},
	computed: {
		canQuery() {
			return this.params.gender && this.params.age && this.params.bodyPart
		},
		
		displayAge() {
			return this.params.age || '选择年龄段'
		},
		
		currentSuggestions() {
			if (!this.healthResult || !this.healthResult.suggestions) return []
			
			const tabNames = ['diet', 'exercise', 'sleep', 'care']
			const currentTab = tabNames[this.activeSuggestionTab]
			return this.healthResult.suggestions[currentTab] || []
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
		},
		
		goBack() {
			uni.navigateBack()
		},
		
		selectGender(gender) {
			this.params.gender = gender
		},
		
		onAgeChange(e) {
			this.ageIndex = e.detail.value
			this.params.age = this.ageRanges[e.detail.value]
		},
		
		selectBodyPart(part) {
			this.params.bodyPart = part
		},
		
		switchSuggestionTab(index) {
			this.activeSuggestionTab = index
		},
		
		async queryHealth() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请完善基本信息',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const result = await this.toolsService.getHealthInfo(this.params)
				
				if (result.success) {
					this.healthResult = result.data
					uni.showToast({
						title: '分析完成',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '分析失败')
				}
			} catch (error) {
				console.error('健康分析失败:', error)
				uni.showToast({
					title: '分析失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		},
		
		getPriorityText(priority) {
			const priorities = {
				'high': '重要',
				'medium': '一般',
				'low': '建议'
			}
			return priorities[priority] || '建议'
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.back-button {
		width: 80rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 40rpx;
		margin-right: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
		
		.back-icon {
			font-size: 32rpx;
			color: #000000;
			font-weight: bold;
		}
	}
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
			margin-bottom: 8rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
			margin-bottom: 8rpx;
		}
		
		.tool-subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.input-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.form-item {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.gender-selector {
			display: flex;
			gap: 16rpx;
			
			.gender-item {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				transition: all 0.3s ease;
				
				&.active {
					border-color: #BD10E0;
					background: linear-gradient(135deg, #BD10E0 0%, #9013FE 100%);
					
					.gender-icon,
					.gender-text {
						color: white;
					}
				}
				
				.gender-icon {
					font-size: 40rpx;
					margin-bottom: 8rpx;
				}
				
				.gender-text {
					font-size: 24rpx;
					color: #000000;
				}
			}
		}
		
		.age-picker {
			.picker-display {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 88rpx;
				padding: 0 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				
				.picker-text {
					font-size: 28rpx;
					color: #000000;
				}
				
				.picker-arrow {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
				}
			}
		}
		
		.body-parts {
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 12rpx;
			
			.part-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 16rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				transition: all 0.3s ease;
				
				&.active {
					border-color: #BD10E0;
					background: linear-gradient(135deg, #BD10E0 0%, #9013FE 100%);
					
					.part-icon,
					.part-name {
						color: white;
					}
				}
				
				.part-icon {
					font-size: 32rpx;
					margin-bottom: 8rpx;
				}
				
				.part-name {
					font-size: 22rpx;
					color: #000000;
				}
			}
		}
		
		.symptoms-input {
			width: 100%;
			min-height: 120rpx;
			padding: 16rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
			line-height: 1.5;
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.result-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.health-analysis {
		.overall-health {
			display: flex;
			align-items: center;
			margin-bottom: 32rpx;
			padding: 32rpx;
			background: linear-gradient(135deg, #BD10E0 0%, #9013FE 100%);
			border-radius: 16rpx;
			
			.health-score {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 32rpx;
				
				.score-circle {
					width: 120rpx;
					height: 120rpx;
					border: 4rpx solid rgba(255, 255, 255, 0.3);
					border-radius: 50%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin-bottom: 12rpx;
					
					.score-text {
						font-size: 36rpx;
						font-weight: bold;
						color: white;
					}
					
					.score-unit {
						font-size: 20rpx;
						color: rgba(255, 255, 255, 0.8);
					}
				}
				
				.score-level {
					font-size: 24rpx;
					color: white;
					font-weight: 500;
				}
			}
			
			.health-desc {
				flex: 1;
				
				.desc-text {
					font-size: 28rpx;
					color: white;
					line-height: 1.6;
				}
			}
		}
		
		.health-details {
			display: flex;
			flex-direction: column;
			gap: 16rpx;
			
			.detail-item {
				padding: 24rpx;
				background-color: #f8f9fa;
				border-radius: 12rpx;
				position: relative;
				
				.detail-header {
					display: flex;
					align-items: center;
					margin-bottom: 12rpx;
					
					.detail-icon {
						font-size: 24rpx;
						margin-right: 12rpx;
					}
					
					.detail-title {
						font-size: 28rpx;
						font-weight: 500;
						color: #000000;
					}
				}
				
				.detail-content {
					font-size: 26rpx;
					color: rgba(0, 0, 0, 0.8);
					line-height: 1.5;
					margin-bottom: 12rpx;
				}
				
				.detail-level {
					position: absolute;
					top: 20rpx;
					right: 20rpx;
					padding: 8rpx 16rpx;
					border-radius: 20rpx;
					
					&.good {
						background-color: #4CAF50;
					}
					
					&.normal {
						background-color: #FF9800;
					}
					
					&.attention {
						background-color: #F44336;
					}
					
					.level-text {
						font-size: 20rpx;
						color: white;
						font-weight: 500;
					}
				}
			}
		}
	}
}

.suggestions-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.suggestions-tabs {
		display: flex;
		margin-bottom: 24rpx;
		background-color: #f8f9fa;
		border-radius: 12rpx;
		padding: 8rpx;
		
		.tab-item {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 16rpx;
			border-radius: 8rpx;
			transition: all 0.3s ease;
			
			&.active {
				background: linear-gradient(135deg, #BD10E0 0%, #9013FE 100%);
				
				.tab-icon,
				.tab-name {
					color: white;
				}
			}
			
			.tab-icon {
				font-size: 24rpx;
				margin-bottom: 4rpx;
			}
			
			.tab-name {
				font-size: 22rpx;
				color: #000000;
			}
		}
	}
	
	.suggestions-content {
		.suggestion-item {
			margin-bottom: 20rpx;
			padding: 20rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.suggestion-header {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 12rpx;
				
				.suggestion-title {
					font-size: 28rpx;
					font-weight: 500;
					color: #000000;
				}
				
				.suggestion-priority {
					padding: 4rpx 12rpx;
					border-radius: 12rpx;
					
					&.high {
						background-color: #F44336;
					}
					
					&.medium {
						background-color: #FF9800;
					}
					
					&.low {
						background-color: #4CAF50;
					}
					
					.priority-text {
						font-size: 20rpx;
						color: white;
						font-weight: 500;
					}
				}
			}
			
			.suggestion-content {
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.8);
				line-height: 1.5;
			}
		}
	}
}

.warnings-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.warnings-list {
		margin-bottom: 24rpx;
		
		.warning-item {
			display: flex;
			align-items: flex-start;
			margin-bottom: 16rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.warning-icon {
				margin-right: 12rpx;
				margin-top: 2rpx;
				
				.icon-text {
					font-size: 24rpx;
				}
			}
			
			.warning-text {
				flex: 1;
				font-size: 26rpx;
				color: #F44336;
				line-height: 1.5;
			}
		}
	}
	
	.disclaimer {
		padding: 16rpx;
		background-color: #fff3e0;
		border-radius: 8rpx;
		border-left: 4rpx solid #FF9800;
		
		.disclaimer-text {
			font-size: 24rpx;
			color: #E65100;
			line-height: 1.4;
		}
	}
}
</style>